package {{wis-controllerPackage}};

{{^java8}}
{{#imports}}import {{import}};
{{/imports}}
import {{wis-responseResultQualifiedName}};
{{#wis-containsComponentTag}}
import {{wis-componentDataQualifiedName}};{{/wis-containsComponentTag}}
{{/java8}}
import org.springframework.beans.factory.annotation.Autowired;
import {{wis-interfaceImport}};
import {{wis-serviceImport}};
{{^java8}}
import org.springframework.web.bind.annotation.*;
{{#async}}
import java.util.concurrent.Callable;{{/async}}{{/java8}}
{{#wis-containsBodyParameter}}{{^wis-containsListType}}import java.util.List;{{/wis-containsListType}}
import java.util.ArrayList;
import javax.validation.Valid;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.FieldError;
import com.wisedu.wecloud.commons.validator.FieldValidateError;{{/wis-containsBodyParameter}}
{{#wis-containsDateQueryParameter}}import org.springframework.format.annotation.DateTimeFormat;{{/wis-containsDateQueryParameter}}

@RestController
{{#operations}}
public class {{wis-controllerClassName}} implements {{wis-interfaceClassName}} {

	@Autowired
	private {{wis-serviceClassName}} service;
{{^java8}}{{#operation}}
    public {{#async}}Callable<{{/async}}ResponseResult<{{{returnType}}}>{{#async}}>{{/async}} {{operationId}}({{#allParams}}{{>queryParamsController}}{{>pathParamsController}}{{>headerParamsController}}{{>bodyParamsController}}{{>formParamsController}}{{#hasMore}},
        {{/hasMore}}{{/allParams}}) {
        // do some magic!{{^async}}
        {{#bodyParam}}{{#isBodyParam}}if (bindingResult.hasErrors()) {
    		List<ObjectError> allErrors = bindingResult.getAllErrors();
    		List<FieldValidateError> fieldValidateErrors = new ArrayList<FieldValidateError>(allErrors.size());
    		FieldValidateError fieldValidateError = null;
    		for (ObjectError objectError : allErrors) {
    			FieldError fieldError = (FieldError) objectError;
    			fieldValidateError = new FieldValidateError();
    			fieldValidateError.setField(fieldError.getField());
    			fieldValidateError.setMessage(fieldError.getDefaultMessage());
    			fieldValidateError.setValidator(fieldError.getCode());
    			fieldValidateErrors.add(fieldValidateError);
    		}
    		return ResponseResult.failure(fieldValidateErrors, "Invalid Parameter");
    	}{{/isBodyParam}}{{/bodyParam}}
        return service.{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}){{/async}};{{#async}}
        return new Callable<ResponseResult<{{{returnType}}}>>() {
            @Override
            public ResponseResult<{{{returnType}}}> call() throws Exception {
                return service.{{operationId}}({{#allParams}}{{paramName}} {{#hasMore}},{{/hasMore}}{{/allParams}});
            }
        };{{/async}}
    }
{{/operation}}{{/java8}}
}
{{/operations}}
